import React from "react";
import "./NewProject.scss";
import { HashRouter as Router, Route, Link } from "react-router-dom";
import AreaData from "../../../component/areaData/areaData.js"; //地址库
import utils from "../../../utils/utils";
import cookie from "react-cookies";
import BMap from "BMap";
import moment from "moment";
import BreadcrumbNav from "../../../component/breadcrumb/breadcrumb.js";
import {
  Steps,
  Col,
  Row,
  message,
  Form,
  Input,
  Button,
  Checkbox,
  DatePicker,
  Select,
  Upload,
  Modal,
} from "antd";
import BarrierGate from "../../../component/progress/progress.js";
import NextMenu from "../../../component/nextMenu/nextMenu";
import {
  LoadingOutlined,
  PlusOutlined,
  UploadOutlined,
} from "@ant-design/icons";
import { fileUpload } from "../../../api/extra";
import {
  getUserPositionInfo,
  addUserAndAuthority,
  updUserAndAuthority,
  getAllAccounnt,
} from "../../../api/accountManage";
import { addProject } from "../../../api/hardwareCenter";
import store from "../../../redux/store";
import NewProgress from "../newProgress/newProgress";
import NewProgress2 from "../newProgress2/newProgress2";
import NewProgress3 from "../newProgress3/newProgress3";

const { Step } = Steps;
const { Option } = Select;

class NewProject extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      activeUlId: "133",
      current: 0,
      steps: [
        {
          title: "项目基本信息",
        },
        {
          title: "项目状态",
        },
        {
          title: "设备配备",
        },
      ],
      OptionOneStep:[],
      OptionSecondStep:[],
      OptionThirdStep:[]
    };
  }

  componentDidMount() {}

  // 下一步
  next = (childState) => {
    console.log('childState',childState);
    if(!childState.projectName){
      message.error("请完善项目名称！")
      return false
    }
    if(!childState.ProComy){
      message.error("请完善项目公司！")
      return false
    }
    if(!childState.allAddress[3]){
      message.error("请完善项目地址详细信息！")
      return false
    }
    if(!childState.signDate){
      message.error("请选择签约时间！")
      return false
    }
    if(!childState.project_phase){
      message.error("请完善项目当前阶段！")
      return false
    }
    if(!childState.admin_phone){
      message.error("请输入管理员账号！")
      return false
    }
    if(!utils.isMobile(childState.admin_phone)){ message.warn('手机号格式不正确，请重新输入'); return; }
    if(!childState.lane_count || !childState.lane_in_count || !childState.lane_out_count){
      message.error("请完善车道数信息！")
      return false
    }
   
    if((childState.fileList.length==0) || (childState.fileList2.length==0)){
      message.error('请上传产区图和合同附件')
      return false
    }
    if(!childState.pro_review_id){
      message.error('请选择项目复核人！')
      return false
    }


    const { current } = this.state;
    this.setState({
      current: current + 1,
      OptionOneStep:childState
    });
  };
  // 下一步
  next1 = (childState1) => {
    console.log('childState1',childState1);
    const { current } = this.state;
    
    
    this.setState({
      current: current + 1,
      OptionSecondStep:childState1
    });
  };
  // 上一步
  prev1 = (childrenState1) => {
    const { current } = this.state;
    console.log('childrenState1-prev',childrenState1)
    this.setState({
      current: current - 1,
      OptionSecondStep:childrenState1
    });
  };
  // 上一步
  prev2 = (childrenState2) => {
    const { current } = this.state;
    console.log('childrenState2-prev',childrenState2)
    this.setState({
      current: current - 1,
      OptionThirdStep:childrenState2
    });
  };

  callback=(msg)=>{
    console.log('父传过来的',msg)
    this.setState({
      OptionThirdStep:msg
    },()=>{
      this.topublish()
    })
    
  }
  // 
  topublish = () => {
    // 如果SaaS数量为2，不限， 不传count
    if(this.state.OptionThirdStep.saas_ac_is_limit == '2'){
      let post_data = {
        'auth_token':cookie.load('station_console_setting_web_login').auth_token,
        'project_name': this.state.OptionOneStep.projectName || '',
        'project_company': this.state.OptionOneStep.ProComy || '',
        'province': this.state.OptionOneStep.allAddress[0] || '',
        'city': this.state.OptionOneStep.allAddress[1] || '',
        'area': this.state.OptionOneStep.allAddress[2] || '',
        'address': this.state.OptionOneStep.allAddress[3] || '' ,
        'sign_time': this.state.OptionOneStep.signDate || '',
        'user_admin_account': this.state.OptionOneStep.admin_phone || '',
        'project_phase': this.state.OptionOneStep.project_phase || '',
        'lane_count': this.state.OptionOneStep.lane_count || '',
        'lane_in_count': this.state.OptionOneStep.lane_in_count || '',
        'lane_out_count': this.state.OptionOneStep.lane_out_count || '',
        'factory_img_ids': JSON.stringify(this.state.OptionOneStep.fileList) || '',
        'contract_img_ids': JSON.stringify(this.state.OptionOneStep.fileList2) || '',
        'create_u_id': this.state.OptionOneStep.pro_creater_id || '',
        'create_u_name': this.state.OptionOneStep.pro_creater || '',
        'review_u_id': this.state.OptionOneStep.pro_review_id || '',
        'review_u_name': this.state.OptionOneStep.pro_review || '',
        'ip': JSON.stringify(this.state.OptionOneStep.IP_info) || '',
        'saas_menu_str': (this.state.OptionThirdStep.otherType).join(',') || '',
        'infrastructure_img_ids': JSON.stringify(this.state.OptionSecondStep.fileList) || '',
        'construct_start_time': this.state.OptionSecondStep.start_time || '',
        'construct_end_time': this.state.OptionSecondStep.end_time || '',
        'construct_contract_img_ids': JSON.stringify(this.state.OptionSecondStep.fileList2) || '',
        'collect_img_ids': JSON.stringify(this.state.OptionSecondStep.fileList3) || '',
        'software_dev_start_time': this.state.OptionSecondStep.start_time_dev || '',
        'software_dev_end_time': this.state.OptionSecondStep.end_time_dev || '',
        'maintenance_end_date': this.state.OptionSecondStep.maintenance_end_date || '',
        'eq': JSON.stringify(this.state.OptionThirdStep.list_info) || '',
        'own_eq': JSON.stringify(this.state.OptionThirdStep.self_info )|| '',
        'saas_ac_is_limit':this.state.OptionThirdStep.saas_ac_is_limit || ''
      }
      console.log('新建项目数据',post_data)
      addProject(post_data).then(res => {
        if(res.code == 0){
          message.success('新建项目成功！',2,()=>{
            window.location.href="/index/hardwareCenter/programConfig"
          })
        }
        console.log(res)
      })
    }else if(this.state.OptionThirdStep.saas_ac_is_limit == '1'){
      let post_data = {
        'auth_token':cookie.load('station_console_setting_web_login').auth_token,
        'project_name': this.state.OptionOneStep.projectName || '',
        'project_company': this.state.OptionOneStep.ProComy || '',
        'province': this.state.OptionOneStep.allAddress[0] || '',
        'city': this.state.OptionOneStep.allAddress[1] || '',
        'area': this.state.OptionOneStep.allAddress[2] || '',
        'address': this.state.OptionOneStep.allAddress[3] || '' ,
        'sign_time': this.state.OptionOneStep.signDate || '',
        'user_admin_account': this.state.OptionOneStep.admin_phone || '',
        'project_phase': this.state.OptionOneStep.project_phase || '',
        'lane_count': this.state.OptionOneStep.lane_count || '',
        'lane_in_count': this.state.OptionOneStep.lane_in_count || '',
        'lane_out_count': this.state.OptionOneStep.lane_out_count || '',
        'factory_img_ids': JSON.stringify(this.state.OptionOneStep.fileList) || '',
        'contract_img_ids': JSON.stringify(this.state.OptionOneStep.fileList2) || '',
        'create_u_id': this.state.OptionOneStep.pro_creater_id || '',
        'create_u_name': this.state.OptionOneStep.pro_creater || '',
        'review_u_id': this.state.OptionOneStep.pro_review_id || '',
        'review_u_name': this.state.OptionOneStep.pro_review || '',
        'ip': JSON.stringify(this.state.OptionOneStep.IP_info) || '',
        'saas_menu_str': (this.state.OptionThirdStep.otherType).join(',') || '',
        'saas_ac_limit_count': this.state.OptionThirdStep.saasType || "",
        'infrastructure_img_ids': JSON.stringify(this.state.OptionSecondStep.fileList) || '',
        'construct_start_time': this.state.OptionSecondStep.start_time || '',
        'construct_end_time': this.state.OptionSecondStep.end_time || '',
        'construct_contract_img_ids': JSON.stringify(this.state.OptionSecondStep.fileList2) || '',
        'collect_img_ids': JSON.stringify(this.state.OptionSecondStep.fileList3) || '',
        'software_dev_start_time': this.state.OptionSecondStep.start_time_dev || '',
        'software_dev_end_time': this.state.OptionSecondStep.end_time_dev || '',
        'maintenance_end_date': this.state.OptionSecondStep.maintenance_end_date || '',
        'eq': JSON.stringify(this.state.OptionThirdStep.list_info) || '',
        'own_eq': JSON.stringify(this.state.OptionThirdStep.self_info )|| '',
        'saas_ac_is_limit':this.state.OptionThirdStep.saas_ac_is_limit || ''
      }
      console.log('新建项目数据',post_data)
      addProject(post_data).then(res => {
        if(res.code == 0){
          message.success('新建项目成功！',2,()=>{
            window.location.href="/index/hardwareCenter/programConfig"
          })
        }
        console.log(res)
      })
    }
    
    
    
  }

  render() {
    const {
      data,
      activeUlId,
      dataSource,
      current,
      steps,
      pro_creater_id,
      previewVisible,
      previewImage,
      admin_account,
      pro_creater,
      pro_review,
      fileList,
      fileList2,
      previewTitle,
      lane_count,
      IP_info,
      lane_out_count,
      lane_in_count,
      admin_phone,
      loading,
      project_phase,
      imageUrl,
      projectName,
      ProComy,
      allAddress,
      signDate,
    } = this.state;
    const uploadButton = (
      <div>
        <PlusOutlined />
        <div style={{ marginTop: 8 }}>Upload</div>
      </div>
    );

    return (
      <div id="ContentMainList" className="NewProject">
        <div>
          <BreadcrumbNav />
        </div>
        <Steps current={current}> 
          {steps.map((item, index) => (
            <Step key={index} title={item.title} />
          ))}
        </Steps>

        {current == 0 ? (
          <div className="steps-content">
            <NewProgress next={this.next} OptionOneStep={this.state.OptionOneStep}  />
          </div>
        ) : current == 1 ? (
          <div className="steps-content">
            <NewProgress2 next1={this.next1} prev1={this.prev1} sign_time={this.state.OptionOneStep.signDate}  project_phase = {this.state.OptionOneStep.project_phase} OptionSecondStep={this.state.OptionSecondStep} /> 
          </div>
        ) : (
          <div className="steps-content">
            <NewProgress3 prev2={this.prev2} OptionThirdStep={this.state.OptionThirdStep} callback={this.callback} />
          </div>
        )}
        <div className="steps-action">
          {/* {current < steps.length - 1 && (
                    <Button type="primary" style={{padding:'10px',lineHeight:'3px',display:'inline-block',marginLeft:'30px'}} onClick={this.next.bind(this)}>
                        下一步
                    </Button>
                    )}
                    {current === steps.length - 1 && (
                    <Button style={{padding:'10px',lineHeight:'3px',display:'inline-block',marginLeft:'30px'}} type="primary" onClick={() => message.success('Processing complete!')}>
                        创建
                    </Button>
                    )}
                    {current > 0 && (
                    <Button style={{padding:'10px',lineHeight:'3px',display:'inline-block',marginLeft:'30px'}} style={{ margin: '0 8px' }} onClick={this.prev.bind(this)}>
                        上一步
                    </Button>
                    )} */}
        </div>
      </div>
    );
  }
}

export default NewProject;
